package com.claimSettlement.service.impl;

import com.claimSettlement.mapper.CaseMapper;
import com.claimSettlement.pojo.Case;
import com.claimSettlement.pojo.Worker;
import com.claimSettlement.service.CaseService;
import com.claimSettlement.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出险Service实现类
 * 汤英杰
 */
@Service
public class CaseServiceImpl implements CaseService {
    @Resource
    CaseMapper mapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    public List<Case> getCaseAll(){
        redisTemplate.delete("caseList");
        List<Case> caseList=null;
        if(redisTemplate.hasKey("caseList")){
            caseList= JsonUtils.jsonToList(redisTemplate.opsForHash().values("caseList").toString(),Case.class);
        }else{
            caseList=mapper.getCaseAll();
            Map<String,Object> map=new HashMap<String, Object>();
            for (Case cases:caseList
                 ) {
                map.put(cases.getCaseId(),JsonUtils.objectToJson(cases));
            }
            redisTemplate.opsForHash().putAll("caseList",map);
        }

        return caseList;
    }

    public int insertCase(Case cases) {
        mapper.updateNum();
        cases.setCaseId(mapper.getLius2());
        int count = mapper.insertCase(cases);
        if(count>0){
            redisTemplate.opsForHash().put("caseList",cases.getCaseId(),JsonUtils.objectToJson(cases));
        }
        return count;
    }

    public int updCaseHandling(int caseHandling,int caseState, String caseId) {
        int count=mapper.updCaseHandling(caseHandling, caseState,caseId);
        if(count>0){
            Case cases=mapper.getCaseOne(caseId);
            redisTemplate.opsForHash().put("caseList",caseId,JsonUtils.objectToJson(cases));
        }
        return count;
    }

    public int insertWorker(String caseId, int userId,String dispatchedWorkerEndTime) {
        int count=mapper.insertWorker(caseId, userId,dispatchedWorkerEndTime);
        if(count>0){
            List<Worker> workerList=null;
            if(redisTemplate.hasKey("workerList")){
                workerList= JsonUtils.jsonToList(redisTemplate.opsForHash().values("workerList").toString(),Worker.class);
            }else{
                workerList=mapper.getWorkerAll();
                Map<String,Object> map=new HashMap<String, Object>();
                for (Worker worker:workerList
                ) {
                    String dispatchedWorkerId= String.valueOf(worker.getDispatchedWorkerId());
                    map.put(dispatchedWorkerId,JsonUtils.objectToJson(worker));
                }
                redisTemplate.opsForHash().putAll("workerList",map);
            }
        }
        return count;
    }

    public Case getCaseOne(String caseId) {
        return mapper.getCaseOne(caseId);
    }

    public List<Case> getCaseId(String caseId, String recognizeeName, String plateNumber) {
        return mapper.getCaseId(caseId, recognizeeName, plateNumber);
    }

    public int updateCase(Case cases) {
        return mapper.updateCase(cases);
    }

    public List<Case> getCaseChul() {
        return mapper.getCaseChul();
    }

    public List<Worker> getWorkerAll() {
        redisTemplate.delete("workerList");
        List<Worker> workerList=null;
        if(redisTemplate.hasKey("workerList")){
            workerList= JsonUtils.jsonToList(redisTemplate.opsForHash().values("workerList").toString(),Worker.class);
        }else{
            workerList=mapper.getWorkerAll();
            Map<String,Object> map=new HashMap<String, Object>();
            for (Worker worker:workerList
            ) {
                String dispatchedWorkerId= String.valueOf(worker.getDispatchedWorkerId());
                map.put(dispatchedWorkerId,JsonUtils.objectToJson(worker));
            }
            redisTemplate.opsForHash().putAll("workerList",map);
        }
        return workerList;
    }


    public int getSurveyCountCaseId(String caseId) {
        return mapper.getSurveyCountCaseId(caseId);
    }

    public List<Case> getCaseByUserId(int userId,int shuzhi,int inbh,int endtime) {
        return mapper.getCaseByUserId(userId, shuzhi, inbh, endtime);
    }

    public int getCaseCount(String recognizeeId, String plateNumber) {
        return mapper.getCaseCount(recognizeeId, plateNumber);
    }

    public Case getCaseBycaseId(String caseId) {
        return mapper.getCaseBycaseId(caseId);
    }

}
